# -*- coding: utf-8 -*-
# @Time    : 2020/10/12 9:23
# @Author  : suhong
# @File    : err_routers.py
# @Software: PyCharm
import platform

from re_common.baselibrary.utils.baseip import BaseIp
from re_common.baselibrary.utils.basetime import BaseTime
from re_common.baselibrary.utils.core.requests_core import SUCCESS

from apps.core.m_route import ContextIncludedRoute

from apps.core.return_info import InputInfoModel, ReturnInfo
from fastapi import APIRouter

from apps.file_uploadfile.models import ErrModel, DataErrModel, DataUserErrModel
from apps.sql_app.mmongodb import MongoDBClient, Coll

router = APIRouter(route_class=ContextIncludedRoute)


@router.post("/err_log/except_client")
async def err_log(err: InputInfoModel[ErrModel]):
    """
    客户端的程序错误
    :param err:
    :return:
    """
    return_info = ReturnInfo()
    # 链接mongodb 表
    # m_result = MongoDBClient.db30_1.client.htmljson.err_msg
    m_result = Coll.get_except_client()
    msg_dict = dict()

    ip_port = str(BaseIp().GetLocalIPByPrefix("192.168"))
    client_ip = err.data.client_ip
    write_time = err.data.write_time
    err_type = err.data.err_type
    err_msg = err.data.err_msg
    traceback_str = err.data.traceback_str
    dicts_item = err.data.dicts_item
    err_source = err.data.err_source
    task_name = err.data.task_name
    step_name = err.data.step_name


    # 自动获取iP_port

    msg_dict['ip_port'] = ip_port
    msg_dict['client_ip'] = client_ip
    msg_dict['write_time'] = write_time
    msg_dict['err_type'] = err_type
    msg_dict['err_msg'] = err_msg
    msg_dict['traceback_str'] = traceback_str
    msg_dict['dicts_item'] = dicts_item
    msg_dict['err_source'] = err_source
    msg_dict['task_name'] = task_name
    msg_dict['step_name'] = step_name
    msg_dict['stat'] = 0
    result = await m_result.insert_one(msg_dict)
    return_info.status = SUCCESS
    return_info.data = {"boos": repr(result.inserted_id)}
    return return_info.todict()


@router.post("/err_log/request_api_fail")
async def err_log(err: InputInfoModel[DataErrModel]):
    """
    客户端请求失败的记录
    :param err:
    :return:
    """
    return_info = ReturnInfo()
    # 链接mongodb 表
    # m_result = MongoDBClient.db30_1.client.htmljson.err_data
    m_result = Coll.get_request_api_fail()
    # ids = BaseTime().get_beijin_date_strins(format="%Y%m%d%H%M%S%f")
    dicts = err.data.dict()
    # dicts["_id"] = ids
    # cjip = await m_result.find_one({"_id": ids})
    # if cjip:
    #     result = await m_result.replace_one({'_id': ids}, dicts)
    #     return_info.status = SUCCESS
    #     return_info.data = {"boos": cjip["_id"]}
    #     return return_info.todict()
    # else:
    result = await m_result.insert_one(dicts)
    return_info.status = SUCCESS
    return_info.data = {"boos": repr(result.inserted_id)}
    return return_info.todict()


@router.post("/err_log/post_user_err")
async def err_log(err: InputInfoModel[DataUserErrModel]):
    """
    用户主动报告错误
    :param err:
    :return:
    """
    return_info = ReturnInfo()
    # 链接mongodb 表
    m_result = Coll.get_post_user_err()
    dicts = err.data.dict()
    result = await m_result.insert_one(dicts)
    return_info.status = SUCCESS
    return_info.data = {"boos": repr(result.inserted_id)}
    return return_info.todict()
